﻿using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using Koala.Pro.Common;
using Koala.Pro.Core.Dto;
using Koala.Pro.Core.Models;
using Koala.Pro.Helpers;
using Koala.Pro.Services;
using Koala.Pro.ViewModels;
using System.Windows;
using System.Windows.Controls;
using Wpf.Ui.Controls;

namespace Koala.Pro.Views.Pages
{
    public partial class FunctionBatchAddPage : System.Windows.Window, INavigableView<FunctionBatchAddViewModel>
    {
        public FunctionBatchAddViewModel ViewModel { get; }

        public FunctionBatchAddPage(FunctionBatchAddViewModel viewModel)
        {
            ViewModel = viewModel;
            base.DataContext = this;
            InitializeComponent();
        }

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);
            ControlHelper.SetDirtyWindow(this);
        }

        [RelayCommand]
        private void Back()
        {
            Close();
        }

        [RelayCommand]
        private async Task Save()
        {
            BatchAddFunctionDto item = ViewModel.Item;
            if (item == null)
            {
                return;
            }
            if (item.Name.IsNullOrWhiteSpace())
            {
                Growl.ErrorGlobal("请输入功能名称");
                txtName.Focus();
                return;
            }
            if (item.ControlID.IsNullOrWhiteSpace())
            {
                Growl.ErrorGlobal("请输入权限标识");
                txtControlID.Focus();
                return;
            }
            if (item.SystemType_ID.IsNullOrWhiteSpace())
            {
                Growl.ErrorGlobal("请输入系统标识");
                txtSystemType_ID.Focus();
                return;
            }
            if (item.PID.IsNullOrWhiteSpace())
            {
                Growl.ErrorGlobal("请选择父级ID");
                FunctionTree.Focus();
                return;
            }
            if (await BLLFactory<IFunctionService>.Instance.IsExistAsync((FunctionInfo s) => s.ControlID == item.ControlID && s.SystemType_ID == item.SystemType_ID))
            {
                MessageDxUtil.ShowTips("指定功能权限标识 重复，请重新输入！");
                txtControlID.Focus();
                return;
            }
            await ViewModel.Save();
            if (ViewModel.Result.Success)
            {
                Back();
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox { IsChecked: var isChecked } checkBox)
            {
                bool valueOrDefault = isChecked.GetValueOrDefault();
                string item = checkBox.Content.ToString();
                List<string> subFunctions = ViewModel.Item.SubFunctions;
                if (valueOrDefault && !subFunctions.Contains(item))
                {
                    subFunctions.Add(item);
                }
                else if (!valueOrDefault && subFunctions.Contains(item))
                {
                    subFunctions.Remove(item);
                }
            }
        }

    }
}
